package com.example.bookexchange;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class HomeActivity extends AppCompatActivity {

    private TextView textViewBookName1, textViewBookName2, textViewBookName3, textViewBookName4;
    private TextView textViewRecyclePrice1, textViewRecyclePrice2, textViewRecyclePrice3, textViewRecyclePrice4;
    private EditText editTextBook1Quantity, editTextBook2Quantity, editTextBook3Quantity, editTextBook4Quantity;
    private TextView textViewTotalBooks, textViewEstimatedPrice;
    private ImageView imageViewBook1, imageViewBook2, imageViewBook3, imageViewBook4;
    private ImageButton buttonDecrease1, buttonIncrease1, buttonDecrease2, buttonIncrease2, buttonDecrease3, buttonIncrease3, buttonDecrease4, buttonIncrease4;

    private Book book1, book2, book3, book4;

    private int[] bookImages = {R.drawable.book1, R.drawable.book2, R.drawable.book3, R.drawable.book4};  // 添加书籍图片数组

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        // 初始化视图
        textViewBookName1 = findViewById(R.id.textViewBookName1);
        textViewBookName2 = findViewById(R.id.textViewBookName2);
        textViewBookName3 = findViewById(R.id.textViewBookName3);
        textViewBookName4 = findViewById(R.id.textViewBookName4);

        textViewRecyclePrice1 = findViewById(R.id.textViewRecyclePrice1);
        textViewRecyclePrice2 = findViewById(R.id.textViewRecyclePrice2);
        textViewRecyclePrice3 = findViewById(R.id.textViewRecyclePrice3);
        textViewRecyclePrice4 = findViewById(R.id.textViewRecyclePrice4);

        editTextBook1Quantity = findViewById(R.id.editTextBook1Quantity);
        editTextBook2Quantity = findViewById(R.id.editTextBook2Quantity);
        editTextBook3Quantity = findViewById(R.id.editTextBook3Quantity);
        editTextBook4Quantity = findViewById(R.id.editTextBook4Quantity);

        textViewTotalBooks = findViewById(R.id.textViewTotalBooks);
        textViewEstimatedPrice = findViewById(R.id.textViewEstimatedPrice);

        imageViewBook1 = findViewById(R.id.imageViewBook1);
        imageViewBook2 = findViewById(R.id.imageViewBook2);
        imageViewBook3 = findViewById(R.id.imageViewBook3);
        imageViewBook4 = findViewById(R.id.imageViewBook4);

        buttonDecrease1 = findViewById(R.id.buttonDecrease1);
        buttonIncrease1 = findViewById(R.id.buttonIncrease1);
        buttonDecrease2 = findViewById(R.id.buttonDecrease2);
        buttonIncrease2 = findViewById(R.id.buttonIncrease2);
        buttonDecrease3 = findViewById(R.id.buttonDecrease3);
        buttonIncrease3 = findViewById(R.id.buttonIncrease3);
        buttonDecrease4 = findViewById(R.id.buttonDecrease4);
        buttonIncrease4 = findViewById(R.id.buttonIncrease4);

        // 创建书籍对象并指定固定价格
        book1 = new Book("兄弟", 0, 54.5);
        book2 = new Book("在细雨中呐喊", 0, 40.7);
        book3 = new Book("许三观卖血记", 0, 40.7);
        book4 = new Book("文城", 0, 39.50);

        // 设置书籍信息
        imageViewBook1.setImageResource(bookImages[0]);
        textViewBookName1.setText(book1.name);
        textViewRecyclePrice1.setText(" ￥ " + book1.price);
        editTextBook1Quantity.setText(String.valueOf(book1.quantity));

        imageViewBook2.setImageResource(bookImages[1]);
        textViewBookName2.setText(book2.name);
        textViewRecyclePrice2.setText(" ￥ " + book2.price);
        editTextBook2Quantity.setText(String.valueOf(book2.quantity));

        imageViewBook3.setImageResource(bookImages[2]);
        textViewBookName3.setText(book3.name);
        textViewRecyclePrice3.setText(" ￥ " + book3.price);
        editTextBook3Quantity.setText(String.valueOf(book3.quantity));

        imageViewBook4.setImageResource(bookImages[3]);
        textViewBookName4.setText(book4.name);
        textViewRecyclePrice4.setText(" ￥ " + book4.price);
        editTextBook4Quantity.setText(String.valueOf(book4.quantity));

        // 设置按钮点击事件
        setButtonListeners();

        // 设置“下一步”按钮点击事件
        Button buttonNext = findViewById(R.id.buttonNext);
        buttonNext.setOnClickListener(v -> {
            // 获取用户选择的数量
            int quantity1 = Integer.parseInt(editTextBook1Quantity.getText().toString());
            int quantity2 = Integer.parseInt(editTextBook2Quantity.getText().toString());
            int quantity3 = Integer.parseInt(editTextBook3Quantity.getText().toString());
            int quantity4 = Integer.parseInt(editTextBook4Quantity.getText().toString());

            // 将书籍信息传递给 OrderDetailsActivity
            Intent intent = new Intent(HomeActivity.this, OrderDetailsActivity.class);
            intent.putExtra("bookNames", new String[]{book1.name, book2.name, book3.name, book4.name});
            intent.putExtra("bookPrices", new double[]{book1.price, book2.price, book3.price, book4.price});
            intent.putExtra("bookQuantities", new int[]{quantity1, quantity2, quantity3, quantity4});
            intent.putExtra("bookImages", bookImages);  // 传递图片数组

            startActivity(intent);
        });
    }

    private void setButtonListeners() {
        buttonDecrease1.setOnClickListener(v -> updateQuantity(v, book1, editTextBook1Quantity));
        buttonIncrease1.setOnClickListener(v -> updateQuantity(v, book1, editTextBook1Quantity));

        buttonDecrease2.setOnClickListener(v -> updateQuantity(v, book2, editTextBook2Quantity));
        buttonIncrease2.setOnClickListener(v -> updateQuantity(v, book2, editTextBook2Quantity));

        buttonDecrease3.setOnClickListener(v -> updateQuantity(v, book3, editTextBook3Quantity));
        buttonIncrease3.setOnClickListener(v -> updateQuantity(v, book3, editTextBook3Quantity));

        buttonDecrease4.setOnClickListener(v -> updateQuantity(v, book4, editTextBook4Quantity));
        buttonIncrease4.setOnClickListener(v -> updateQuantity(v, book4, editTextBook4Quantity));
    }

    private void updateQuantity(View v, Book book, EditText editText) {
        int currentQuantity = Integer.parseInt(editText.getText().toString());
        if (v.getId() == R.id.buttonDecrease1 || v.getId() == R.id.buttonDecrease2 || v.getId() == R.id.buttonDecrease3 || v.getId() == R.id.buttonDecrease4) {
            if (currentQuantity > 0) {
                currentQuantity--;
            }
        } else {
            if (currentQuantity < 10) { // 限制最大数量为10
                currentQuantity++;
            }
        }
        editText.setText(String.valueOf(currentQuantity));
        book.quantity = currentQuantity;
        updateTotalAndPrice();
    }

    private void updateTotalAndPrice() {
        int totalBooks = book1.quantity + book2.quantity + book3.quantity + book4.quantity;
        double totalPrice = book1.price * book1.quantity + book2.price * book2.quantity + book3.price * book3.quantity + book4.price * book4.quantity;

        textViewTotalBooks.setText(" " + totalBooks + " ");
        textViewEstimatedPrice.setText(" ￥" + totalPrice);
    }

    private static class Book {
        String name;
        int quantity;
        double price;

        public Book(String name, int quantity, double price) {
            this.name = name;
            this.quantity = quantity;
            this.price = price;
        }
    }
}
